﻿using FineUI;
using SysManage.BLL;
using System;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Common;

namespace SysManage.Module.FilteringRule
{
    public partial class KeyWordEdit : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }
        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                KeyWord keyWord = DB.KeyWords.Find(id);
                if (keyWord == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                txtWordText.Text = keyWord.WordText;
                txtDescription.Text = keyWord.Description;
            }
        }
        #endregion

        #region Events

        private void SaveData()
        {
            string id = GetQueryIntValue("id");
            string imageUrl = string.Empty;

            if (id != "")
            {
                //更新
                KeyWord keyWord = DB.KeyWords.Find(id);
                if (keyWord == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                keyWord.WordText = txtWordText.Text.Trim();
                keyWord.Description = txtDescription.Text.Trim();
                keyWord.ModifyTime = DateTime.Now;
                keyWord.ModifyBy = CurrentUser.LoginID;
                
                DB.SaveChanges();
            }
            else
            {
                KeyWord keyWord = new KeyWord();
                keyWord.ID = StringHelper.NewID;
                keyWord.WordText = txtWordText.Text.Trim();
                keyWord.Description = txtDescription.Text.Trim();
                keyWord.CreateTime = DateTime.Now;
                keyWord.ModifyTime = DateTime.Now;
                keyWord.CreateBy = CurrentUser.LoginID;
                keyWord.ModifyBy = CurrentUser.LoginID;
               
                DB.KeyWords.Add(keyWord);

                DB.SaveChanges();
            }
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData();

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
    }
}